import unittest

from survey import AnonymousSurvey


class MyTestCase(unittest.TestCase):
    """
    在每一个test方法中都创建了AnonymousSurvey实例
    """
    # def test_store_single_response(self):
    #     question = '你学会的第一门外语是什么？'
    #     my_survey = AnonymousSurvey(question)
    #     my_survey.store_response('English')
    #     self.assertIn('English', my_survey.responses)
    #
    # def test_store_three_responses(self):
    #     question = '你学会的第一门外语是什么？'
    #     my_survey = AnonymousSurvey(question)
    #     responses = ['English', 'Spanish', 'Japanese']
    #     for response in responses:
    #         my_survey.store_response(response)
    #     for response in responses:
    #         # 判断元素在不在列表里
    #         self.assertIn(response, my_survey.responses)

    """
    在unittest.TestCase类中提供了一个方法setUp()，python将先运行这个方法，再运行后面的test方法，使得创建的一个实例对象可以被后面的多个方法使用
    """

    def setUp(self):
        question = '你学会的第一门语言是什么？'
        self.my_survey = AnonymousSurvey(question)
        self.responses = ['English', 'Spanish', 'Japanese']

    def test_store_single_response(self):
        self.my_survey.store_response('English')
        self.assertIn(self.responses[0], self.my_survey.responses)
        print('可插入单个答案！')

    def test_store_three_responses(self):
        for response in self.responses:
            self.my_survey.store_response(response)
        for response in self.responses:
            # 判断元素在不在列表里
            self.assertIn(response, self.my_survey.responses)
        print('可插入多个答案！')


if __name__ == '__main__':
    unittest.main()
